﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web.Mvc;
using MVC.Models;

namespace MVC.Controllers
{
    [Authorize]
    public class BookListController : Controller
    {
        private readonly BookContext db = new BookContext();

        // GET: /BookList/
        public ActionResult Index()
        {
            var list = db.BookLists.First(l => l.UserName.Equals(User.Identity.Name));
            var books = list.Books.ToList();

            ViewBag.Title = "My Books";

            if (Session["Message"] != null)
            {
                ViewBag.Message = (string)Session["Message"];
                Session.Remove("Message");
            }
            
            return View(books);
        }

        // GET: /BookList/Details/5
        public ActionResult Details(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            var readBook = db.ReadBooks.Find(id);
            if (readBook == null)
            {
                return HttpNotFound();
            }

            ViewBag.Title = readBook.Book.Title;

            return View(readBook);
        }

        // GET: /BookList/Edit/5
        public ActionResult Edit(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            var readBook = db.ReadBooks.Include(b => b.Book).Single(i => i.ReadBookId == id);
            if (readBook == null)
            {
                return HttpNotFound();
            }

            return View(readBook);
        }

        // POST: /BookList/Edit/5
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit([Bind(Include = "ReadBookId,Rating,DateRead,Book,BookListId")] ReadBook readBook)
        {
            if (!ModelState.IsValid) return View(readBook);
            db.Entry(readBook).State = EntityState.Modified;
            db.Entry(readBook.Book).State = EntityState.Modified;
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        [HttpPut]
        public ActionResult Rate(int id, int rating)
        {
            var readBook = db.ReadBooks.FirstOrDefault(b => b.ReadBookId == id);

            if(readBook.Rating == 0)
                readBook.DateRead = DateTime.Now;
            
            readBook.Rating = rating;

            db.Entry(readBook).State = EntityState.Modified;

            db.SaveChanges();

            return RedirectToAction("Index");
        }

        [HttpPut]
        public ActionResult Date(int id, string date)
        {
            var readBook = db.ReadBooks.FirstOrDefault(b => b.ReadBookId == id);

            readBook.DateRead = DateTime.Parse(date);

            db.Entry(readBook).State = EntityState.Modified;

            db.SaveChanges();

            return RedirectToAction("Index");
        }

        // GET: /BookList/Delete/5
        public ActionResult Delete(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            var readBook = db.ReadBooks.Find(id);
            if (readBook == null)
            {
                return HttpNotFound();
            }

            return View(readBook);
        }

        // POST: /BookList/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            var readBook = db.ReadBooks.Find(id);

            Session.Add("Message", "Successfully removed '" + readBook.Book.Title + "' from your list.");

            db.ReadBooks.Remove(readBook);
            db.SaveChanges();

            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }
    }
}